import { specialCharReg, standardReg } from '@/utils/utils';
import { PlusOutlined } from '@ant-design/icons';
import { ModalForm, ProFormText } from '@ant-design/pro-components';
import { Button, message } from 'antd';
import { createETC } from '../service';

const CreateEtc = (props: { reload: () => void }) => {
  return (
    <ModalForm
      title="添加ETC厂商"
      trigger={
        <Button type="primary">
          <PlusOutlined /> 添加ETC厂商
        </Button>
      }
      layout="horizontal"
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 20 }}
      modalProps={{
        destroyOnClose: true,
        maskClosable: false,
      }}
      onFinish={async (values) => {
        const res = await createETC(values);
        if (res.success) {
          message.success({
            content: res.message,
            key: 'add_etc',
          });
          props.reload();
          return true;
        }
        message.error({
          content: res?.message,
          key: 'add_etc',
        });
        return false;
      }}
    >
      <ProFormText
        name="platId"
        label="ETC厂商ID"
        rules={[
          {
            required: true,
            validator: (_, val) => {
              if (val && val.length > 0 && standardReg.test(val)) {
                return Promise.resolve();
              }
              return Promise.reject(new Error('ETC厂商ID不能为空'));
            },
          },
        ]}
      />
      <ProFormText
        required
        name="platName"
        label="ETC厂商名称"
        rules={[
          {
            required: true,
            validator: (_, val) => {
              if (!val || specialCharReg.test(val))
                return Promise.reject(new Error('ETC厂商名称不符合规范，不支持包含特殊字符'));
              return Promise.resolve(val);
            },
          },
        ]}
      />
      <ProFormText name="baseUrl" label="接口地址" />
      <ProFormText name="apiVersion" label="接口版本" />
      <ProFormText name="apiKey" label="apiKey" />
      <ProFormText name="merchantId" label="merchantId" />
      <ProFormText name="uuid" label="uuid" />
      <ProFormText name="appId" label="appId" />
      <ProFormText name="secret" label="secret" />
    </ModalForm>
  );
};

export default CreateEtc;
